package cn.java79.quiz.service;

import cn.java79.quiz.dao.PractiseMapper;
import cn.java79.quiz.dao.UserMapper;
import cn.java79.quiz.entity.Practise;
import cn.java79.quiz.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * @author oldliu
 * @since 1.0
 */
public class PractiseService {
    public static final int PAGESIZE=10;
    public static final int QuizFenzhi=3;
    public static PractiseService ME=new PractiseService();

    //保存practse,扣除3个积分
    public int add(Practise p){
        SqlSession s = MybatisUtil.get();
        try {
            UserMapper userMapper =s.getMapper(UserMapper.class);
            PractiseMapper mapper = s.getMapper(PractiseMapper.class);
            userMapper.decrScore(p.getUid(),QuizFenzhi);
            return  mapper.add(p);
        }finally {
            MybatisUtil.close(s);
        }
    }


    public int addS(List<Practise> practise) {
        SqlSession s = MybatisUtil.get();
        try {
            //UserMapper userMapper =s.getMapper(UserMapper.class);
            PractiseMapper mapper = s.getMapper(PractiseMapper.class);
            for(Practise p:practise){
                //userMapper.decrScore(p.getUid(),QuizFenzhi);
                mapper.add(p);
            }
            return 1;
        }finally {
            MybatisUtil.close(s);
        }
    }
}
